﻿using OF.BLL.Dish;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using OF.Common;
using System.Data;
namespace OF.Web.admin
{
    public partial class orderdetail : ShopAdminPage
    {
        BLL.Order.Order bll_order = new BLL.Order.Order();
        BLL.Dish.DishAdmin bll_dish = new BLL.Dish.DishAdmin();
        string orderid = "0";
        public Model.Orders model_order;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                orderid = Request.QueryString["orderid"];
                if (string.IsNullOrWhiteSpace(orderid))
                    return;
                Bind();
            }
        }

        public void Bind()
        {
            int sellerid = WebUtils.GetId();
            List<OF.Model.Orders> listOrder= bll_order.GetModelList("id=" + orderid + " and sellerid=" + sellerid);
            if (listOrder == null) return;
            model_order = listOrder[0];
            if (model_order == null) return;
            if (model_order.state == (int)Enmus.ORDER_STATE.INIT)
            {
                divbtn.Visible = true;
            }
            else
            {
                divbtn.Visible = false;
            }
            hforderid.Value = model_order.id.ToString();
            hfstate.Value = model_order.state.ToString();

            labordersn.Text = model_order.ordersn;
            labtype.Text = model_order.ordertype == 1 ? "外卖" : "点餐";

            labtime.Text = model_order.scheduledtime;
            labphone.Text = model_order.phone;
            if (model_order.ordertype == 1)
            {
                traddress.Visible = true;
                trcount.Visible = false;
            }
            else
            {
                traddress.Visible = false;
                trcount.Visible = true;
            }
            labaddress.Text = model_order.sendaddress;
            labcount.Text = model_order.population == null ? "" : model_order.population.ToString();
            labremark.Text = model_order.remark;

            DataSet ds = bll_order.GetOrderDetailsById(Convert.ToInt32(orderid));
            rptList.DataSource = ds;
            rptList.DataBind();
        }


        public string GetName(object dishid)
        {
            if (dishid != null)
            {
                Model.Dish model = bll_dish.GetModel(Convert.ToInt32(dishid));
                if (model != null)
                    return model.dishname;
            }
            return string.Empty;

        }

        protected void btnYes_Click(object sender, EventArgs e)
        {
            int state = Convert.ToInt32(hfstate.Value);
            if (bll_order.Update(hforderid.Value, (int)Enmus.ORDER_STATE.CONFIRM_SUC))
            {
                //插入日志
                bll_order.AddOrderLog(state, (int)Enmus.ORDER_STATE.CONFIRM_SUC, Convert.ToInt32(hforderid.Value), 2, WebUtils.GetId());

                OF.Common.MessageBox.ShowAndRedirect(this, "处理订单成功", "orderlist.aspx");
            }
            else
            {
                OF.Common.MessageBox.Show(this, "操作失败");
            }
        }

        protected void btnNo_Click(object sender, EventArgs e)
        {
           
            int state = Convert.ToInt32(hfstate.Value);
            Model.Orders model = bll_order.GetModel(Convert.ToInt32(hforderid.Value));
            model.state=(int)Enmus.ORDER_STATE.CONFIRM_Fail;
            model.reason = hfdesc.Value;
            if (bll_order.Update(model))
            {
                ////调用推送接口
                //BLL.Push.BaiduPush baidu_push = new BLL.Push.BaiduPush();
                //string msg ="对不起，该订单所点的菜品目前不足，请重新点菜，谢谢配合";
                //string result = baidu_push.PushMsg(msg, "点餐提醒", 0, 3, 1, "", "", "");
                //if (!Utils.IsNumeric(result))
                //{
                //    MessageBox.Show(this, "推送消息失败：" + result);
                //    return;
                //}
                //插入日志
                bll_order.AddOrderLog(state, (int)Enmus.ORDER_STATE.CONFIRM_Fail, Convert.ToInt32(hforderid.Value), 2, WebUtils.GetId());

                OF.Common.MessageBox.ShowAndRedirect(this, "该订单将不再处理!", "orderlist.aspx");
            }
            else
            {
                OF.Common.MessageBox.Show(this, "操作失败");
            }
        }
    }
}